package cn.itcast.order.service;

import cn.itcast.client.UserClient;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.pojo.User;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2.远程查询user
        /*//String url = "http://localhost:8081/user/" + order.getUserId(); //2.1.url地址-->硬编码
        String url = "http://userservice/user/" + order.getUserId(); //2.1.url地址-->eureka注册中心获取
        //利用
        User user = restTemplate.getForObject(url, User.class); //2.2.发起调用*/
        //利用Feign发起http请求，查询用户
        User user = userClient.findById(order.getUserId());
        order.setUser(user);    //3.存入order
        // 4.返回
        return order;
    }

    @SentinelResource("goods")
    public void queryGoods() {
        System.err.println("查询商品。。。");
    }
}
